package com.rzz.stack_queue;

import java.util.Stack;

/**
 * @description: 仅使用递归和栈操作逆序栈
 * @author: ruanzhizhi
 * @date: 2021/6/6 22:10
 */
public class ReverseStack {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
//        stack.push(5);
//        stack.push(4);
//        stack.push(3);
        stack.push(2);
        stack.push(1);
        for (Integer i: stack) {
            System.out.println(i);
        }
        reverse(stack);
        System.out.println("=============");
        for (Integer i: stack) {
            System.out.println(i);
        }
    }
    public static int getAndRemoveLastElement(Stack<Integer> stack){
        int result = stack.pop();
        if (stack.isEmpty()){
            return result;
        }else {
            int last = getAndRemoveLastElement(stack);
            stack.push(result);
            return last;
        }
    }
    public static void reverse(Stack<Integer> stack){
        if (stack.isEmpty()){
            return;
        }
        int i = getAndRemoveLastElement(stack);
        reverse(stack);
        stack.push(i);
    }
}
